<?php

namespace app\admin\controller;

use app\admin\model\Banner;
use app\admin\model\BannerItem;
use think\Request;

class BannerController extends BaseController
{
    // 广告位列表页
    public function lst()
    {
        $data = Banner::order('create_time','asc')->paginate(8);
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    //广告位添加
    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new Banner();
                if($model->allowField(true)->save($params)){
                    $this->success('添加广告位置成功!',url('admin/banner/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    //修改广告位信息
    public function edi(){
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',Banner::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new Banner();
                if($model->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改节点成功!',url('admin/banner/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    //删除单个广告位
    public function del()
    {
        $id = $this->request->param('id');
        $data = BannerItem::where('banner_id','eq',$id)->find();
        if($data){
            return '广告位下还有图片没有删除,请先删除广告位下的图片';
        }
        if(Banner::destroy($id)){
            return '广告位删除成功';
        }
        return '删除失败,怎么啦';
    }
}
